/**
 * wiki: https://wiki.upesn.com/pages/viewpage.action?pageId=2396500
 */
import config from '../../config/config'
import * as types from './mutations_types'

const CGI = {
	userInfoUrl: '[javaApi]/users/code',
	authInfoUrl:'[javaApi]/auths/user',
	getpublishclassUrl: '[javaApi]/releasecourse/classes',//1
	getclassattendenceUrl : '[javaApi]/releasecourse/attdetails',//4
	coursepreviewUrl:'[javaApi]/coursepreview/',
	addcourseUrl: '[javaApi]/coursepreview/addcourse',
	searchusersUrl :'[javaApi]/coursepreview/users',
	coursenoticeUrl : '[javaApi]/coursenotice/',
	signincourseUrl : '[javaApi]/coursenotice/signstate',
	createclassUrl:'[javaApi]/releasecourse/createclass',//2
	searchclassusersUrl :'[javaApi]/releasecourse/users',//3
	updateclassUrl:'[javaApi]/releasecourse/updateclass',//5
	getuserinfoUrl:'[javaApi]/releasecourse/getuserinfo',//6
	updateleastateUrl:'[javaApi]/releasecourse/updateleastate',//7
	dropcourseUrl:'[javaApi]/releasecourse/dropcourse',//8
	getmyclassUrl: '[javaApi]/course/getcourse'
}
module.exports = {
	/**
	 * 更新store中的当前用户信息
	 * @params Object {commit, state}
	 * @params Object {vue实例}
	 */
	setUserInfo: ({commit, state}, {vue}) => {
		return new Promise((resolve, reject) => {
			if(!state.userinfo.id){
				var code = vue.$route.query.code
				vue.$http.get(CGI.userInfoUrl+"?code="+code).then((rs) => {
					let data = rs.data || {}
					if(data.code === 200){ //请求成功
						commit(types.UPDATE_USERINFO, data.data)
						resolve(data.data)
					} else {
						reject(data.message)
					}
				}).catch((error) => {
					reject(error)
				})
			} else{
				resolve(state.userinfo)
			}
		})
	},
	getUserAuth:({commit,state},{vue}) => {
		return new Promise((resolve,reject) => {
			if (state.userinfo.id) {
				vue.$http.defaults.headers.common['token'] = state.userinfo.id;
				vue.$http.get(CGI.authInfoUrl).then((rs) => {
					let data = rs.data || {}
					if(data.code === 200){ //请求成功
						commit(types.UPDATE_USERAUTHINFO, data.data)
						resolve(data.data)
					} else {
						reject(data.message)
					}
				}).catch((error) => {
					reject(error)
				})				
			}else{
				resolve(state.userinfo)
			}
		})
	},
	getClassPreview:({commit,state},{vue}) => {
		return new Promise((resolve,reject) => {
			vue.$http.defaults.headers.common['token'] = state.userinfo.id;
			vue.$http.get(CGI.coursepreviewUrl).then((rs) => {
                let data = rs.data || {}
                if (data.code === 200) {
                    commit(types.COURSEPREVIEW,data.data)
                    resolve(data.data)
                }else{
					commit(types.GETERRINFO,data)
                    reject(data.message)
                }
			}).catch((error) => {
				commit(types.GETERRINFO,error)
                reject(error)
            })
		})
	},
	getClassPreviewStudent:({commit,state},{vue,param}) => {
		return new Promise((resolve,reject) => {
			vue.$http.defaults.headers.common['token'] = state.userinfo.id;
			var searchtext = param.searchtext
			var classid = param.classid
			vue.$http.get(CGI.searchusersUrl+"?searchtext="+searchtext+"&class_id="+classid).then((rs) => {
                let data = rs.data || {}
                if (data.code === 200) {
                    commit(types.SEARCHUSER,data.data)
                    resolve(data.data)
                }else{
                    reject(data.message)
                }
			}).catch((error) => {
                reject(error)
            })
		})
	},
	signupClass:({commit,state},{vue,param}) => {
		return new Promise((resolve,reject) => {
			vue.$http.defaults.headers.common['token'] = state.userinfo.id;
			vue.$http.post(CGI.addcourseUrl,{param}).then((rs) => {
                let data = rs.data || {}
                if (data.code === 200) {
                    commit(types.ADDCOURSE)
                    resolve(data.data)
                }else{
					commit(types.GETERRINFO,data)
                    reject(data.message)
                }
			}).catch((error) => {
				commit(types.GETERRINFO,error)
                reject(error)
            })
		})
	},
	getClasseNotice:({commit,state}, {vue}) => {
        return new Promise((resolve, reject) => {
			vue.$http.defaults.headers.common['token'] = state.userinfo.id;
            vue.$http.get(CGI.coursenoticeUrl).then((rs) => {
                let data = rs.data || {}
                if (data.code === 200) {
                    commit(types.COURSENOTICE,data.data)
                    resolve(data.data)
                }else{
					commit(types.GETERRINFO,data)
                    reject(data.message)
                }
            }).catch((error) => {
				commit(types.GETERRINFO,error)
                reject(error)
            })
        })
	},
	signinClass:({commit,state},{vue,param}) => {
        //debugger
		return new Promise((resolve,reject) => {
			vue.$http.defaults.headers.common['token'] = state.userinfo.id;
			//debugger
			vue.$http.post(CGI.signincourseUrl,{param}).then((rs) => {
				//debugger
                let data = rs.data || {}
                if (data.code === 200) {
                    commit(types.CLASSSIGNIN)
                    resolve(data.data)
                }else{
					commit(types.GETERRINFO,data)
                    reject(data.message)
                }
			}).catch((error) => {
				commit(types.GETERRINFO,error)
                reject(error)
            })
		})
	},
	//开课首页1
	getPublishClass:({commit,state}, {vue}) => {
        return new Promise((resolve, reject) => {
			vue.$http.defaults.headers.common['token'] =  state.userinfo.id;
            vue.$http.get(CGI.getpublishclassUrl).then((rs) => {
                let data = rs.data || {}
                if (data.code === 200) {
                    commit(types.GETPUBLISHCLASS,data.data)
                    resolve(data.data)
				}else{
					commit(types.GETERRINFO,data)
                    reject(data)
                }
            }).catch((error) => {
				commit(types.GETERRINFO,error)
                reject(error)
            })
        })
	},
	//创建课程2
	createClass:({commit,state},{vue,param}) => {
		//debugger
		return new Promise((resolve,reject) => {
			vue.$http.defaults.headers.common['token'] = state.userinfo.id;
			//debugger
			vue.$http.post(CGI.createclassUrl,{param}).then((rs) => {
				//debugger
                let data = rs.data || {}
                if (data.code === 200) {
                    commit(types.CREATECLASS)
                    resolve(data.data)
                }else{
					commit(types.GETERRINFO,data)
                    reject(data.message)
                }
			}).catch((error) => {
				commit(types.GETERRINFO,error)
                reject(error)
            })
		})
	},
	//搜索人员3
	searchClassUsers:({commit,state},{vue,type,search}) => {
		//debugger
		return new Promise((resolve,reject) => {
			vue.$http.defaults.headers.common['token'] = state.userinfo.id;
			if(type=='teachers'){
				//debugger
				vue.$http.get(CGI.searchclassusersUrl+"?type="+type+"&search="+search).then((rs) => {
					let data = rs.data || {}
					if (data.code === 200) {
						commit(types.GETPERSONSTEACHERS,data.data)
						resolve(data.data)
					}else{
						reject(data.message)
					}
				}).catch((error) => {
					reject(error)
				})
			}else if(type=='allusers'){
				//debugger
				vue.$http.get(CGI.searchclassusersUrl+"?type="+type+"&search="+search).then((rs) => {
					let data = rs.data || {}
					if (data.code === 200) {
						commit(types.GETPERSONSALLUSERS,data.data)
						resolve(data.data)
					}else{
						reject(data.message)
					}
				}).catch((error) => {
					reject(error)
				})
			}
			
		})
	},
	//查询考情详情4
	getClassAttendence:({commit,state},{vue,subclassid}) => {
		//debugger
		return new Promise((resolve,reject) => {
			vue.$http.defaults.headers.common['token'] = state.userinfo.id;
			vue.$http.get(CGI.getclassattendenceUrl+"/"+subclassid).then((rs) => {
                let data = rs.data || {}
                if (data.code === 200) {
                    commit(types.GETCLASSATTENDENCE,data.data)
                    resolve(data.data)
                }else{
                    reject(data.message)
                }
			}).catch((error) => {
                reject(error)
            })
		})
	},
	//修改课程5
	updateClass:({commit,state},{vue,param}) => {
		//debugger
		return new Promise((resolve,reject) => {
			vue.$http.defaults.headers.common['token'] = state.userinfo.id;
			vue.$http.post(CGI.updateclassUrl,{param}).then((rs) => {
                let data = rs.data || {}
                if (data.code === 200) {
                    commit(types.UPDATECLASS)
                    resolve(data.data)
                }else{
					commit(types.GETERRINFO,data)
                    reject(data.message)
                }
			}).catch((error) => {
				//debugger
				commit(types.GETERRINFO,error)
                reject(error)
            })
		})
	},
	//查看课程人员考勤信息和请假信息6
	getUserInfo:({commit,state},{vue,subclassid,type,search}) => {
		//debugger
		return new Promise((resolve,reject) => {
			vue.$http.defaults.headers.common['token'] = state.userinfo.id;
			//详细信息
			if(type===0){
				//debugger
				vue.$http.get(CGI.getuserinfoUrl+"/"+subclassid+"?type="+type+"&search="+search).then((rs) => {
					let data = rs.data || {}
					if (data.code === 200) {
						commit(types.GETCLASSATTENDENCEDETAIL,data.data)
						resolve(data.data)
					}else{
						reject(data.message)
					}
				}).catch((error) => {
					reject(error)
				})
				//请假
			}else if(type===1){
				//debugger
				vue.$http.get(CGI.getuserinfoUrl+"/"+subclassid+"?type="+type+"&search="+search).then((rs) => {
					let data = rs.data || {}
					if (data.code === 200) {
						commit(types.GETCLASSLEAVEINFO,data.data)
						resolve(data.data)
					}else{
						reject(data.message)
					}
				}).catch((error) => {
					reject(error)
				})
			}
			
		})
	},
	//申请请假7
	updateLeaState:({commit,state},{vue,param}) => {
		return new Promise((resolve,reject) => {
			vue.$http.defaults.headers.common['token'] = state.userinfo.id;
			vue.$http.post(CGI.updateleastateUrl,{param}).then((rs) => {
                let data = rs.data || {}
                if (data.code === 200) {
                    commit(types.LEAVECLASS)
                    resolve(data.data)
                }else{
                    reject(data.message)
                }
			}).catch((error) => {
				//debugger
                reject(error)
            })
		})
	},
	//删除整门课程8
	dropCourse:({commit,state},{vue,param}) => {
		//debugger
		return new Promise((resolve,reject) => {
			vue.$http.defaults.headers.common['token'] = state.userinfo.id;
			vue.$http.post(CGI.dropcourseUrl,{param}).then((rs) => {
                let data = rs.data || {}
                if (data.code === 200) {
                    commit(types.DELETECLASS)
                    resolve(data.data)
                }else{
                    reject(data.message)
                }
			}).catch((error) => {
				//debugger
                reject(error)
            })
		})
	},
	getMyClass:({commit,state},{vue,param}) => {
		return new Promise((resolve,reject) => {
			vue.$http.defaults.headers.common['token'] = state.userinfo.id;
			var pageindex = param.pageindex
			var pagesize = param.pagesize
			var type = param.type
			var appendUrl = "pagesize="+pagesize +"&pageindex="+pageindex + "&type=" + type
			vue.$http.get(CGI.getmyclassUrl+"?"+appendUrl).then((rs) => {
                let data = rs.data || {}
                if (data.code === 200) {
                    commit(types.GETMYCLASS,data.data)
                    resolve(data.data)
                }else{
					commit(types.GETERRINFO,data)
                    reject(data)
                }
			}).catch((error) => {
				commit(types.GETERRINFO,error)
                reject(error)
            })
		})
	}
}
